<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/jsp/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>信息编辑窗体</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="${ctxStatic}/plugins/h+/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/animate.min.css" rel="stylesheet">
    <%--<link href="${ctxStatic}/plugins/h+/css/style.min862f.css?v=4.1.0" rel="stylesheet">--%>
    <link rel="stylesheet" type="text/css" href="${ctxStatic}/plugins/h+/css/plugins/webuploader/webuploader.css">
    <link href="${ctxStatic}/plugins/h+/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">

    <link href="${ctxStatic}/plugins/h+/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/colorpicker/css/bootstrap-colorpicker.min.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/cropper/cropper.min.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/switchery/switchery.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/jasny/jasny-bootstrap.min.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/nouslider/jquery.nouislider.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/ionRangeSlider/ion.rangeSlider.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/ionRangeSlider/ion.rangeSlider.skinFlat.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css"
          rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/plugins/clockpicker/clockpicker.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/animate.min.css" rel="stylesheet">
    <link href="${ctxStatic}/plugins/h+/css/style.min.css?v=4.0.0" rel="stylesheet">
    <base target="_blank">
    <link href="${pageContext.request.contextPath}/resources/plugins/h+/css/plugins/jsTree/style.min.css"
          rel="stylesheet">
    <style>
        .jstree-open > .jstree-anchor > .fa-folder:before {
            content: "\f07c"
        }

        .jstree-default .jstree-icon.none {
            width: 0
        }
    </style>
</head>
<body class="gray-bg">


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="panel-body">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form onsubmit="return  false" class="form-horizontal" id="offlineCardFaFangForm">
                        <input id="offlineCard_id"  name="pofflineCard.id" type="hidden">
                        <input id="offlineCard_BrandId"  name="pofflineCard.brandId" type="hidden">
                        <input id="offlineCard_TypeId"  name="pofflineCard.typeId" type="hidden">
                        <input id="offlineCard_notes"  name="pofflineCard.card_notes" type="hidden">
                        <input id="offlineCard_bizPersonId"  name="pofflineCard.bizPersonId" type="hidden">


                        <div class="form-group">
                            <label class="col-sm-3 control-label">名 称：</label>
                            <div class="col-sm-8">
                                <label class="control-label">${offlineCard.name}</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">券 额：</label>
                            <div class="col-sm-8">
                                <label class="control-label" id="offlineCard_cardTotal"></label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">发放类型：</label>
                            <div class="col-sm-8">
                                <input type="hidden" id="cardCode" name="pofflineCard.cardCode">
                                <input type="hidden" id="cardCodeType" name="pofflineCard.cardCodeType">
                                <select id="offlineCard_type" data-placeholder="选择类型..." class="chosen-select"  class="form-control" onchange="changeType()">
                                    <option value="1">顺序发放</option>
                                    <option value="2">指定发放</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group" id="type1">
                            <label class="col-sm-3 control-label">发放数量：</label>
                            <div class="col-sm-8">
                                <input id="offlineCard_cardCount" value="0" name="pofflineCard.cardCount"
                                       onblur="checkNum(this)" onkeyup="this.value=this.value.replace(/\D/g,'')"  onafterpaste="this.value=this.value.replace(/\D/g,'')"
                                       class="form-control" type="text">
                                <span style="color: red;">目前剩余<span id="syCount"></span>张</span>
                            </div>
                        </div>
                        <div class="form-group" id="type2">
                            <label class="col-sm-3 control-label">选择号段：</label>
                            <div class="col-sm-8">
                                <button class="btn btn-primary" onclick="loadCard();">添加</button>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">选择业务代表：</label>
                            <div class="col-sm-8">
                                <select id="list_bizperson" data-placeholder="选择业务代表..." class="chosen-select"  class="form-control">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">选择商户：</label>
                            <div class="col-sm-8">
                                <select id="list_brand_code" data-placeholder="选择商户..." class="chosen-select"  class="form-control">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">选择类型：</label>
                            <div class="col-sm-8">
                                <select id="list_type_code" data-placeholder="选择类型..." class="chosen-select"  class="form-control">
                                    <option value=""></option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">发放备注：</label>
                            <div class="col-sm-8">
                                <textarea type="text"  id="list_card_notes"
                                          value="" class="form-control" /></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
                                <button class="btn btn-primary" onclick="doSave();">提交</button>
                                <button id="btn_close" class="btn btn-primary" type="button">关闭</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>




<script src="${pageContext.request.contextPath}/resources/plugins/h+/js/jquery.min.js?v=2.1.4"></script>
<script src="${pageContext.request.contextPath}/resources/plugins/h+/js/bootstrap.min.js?v=3.3.6"></script>
<script src="${pageContext.request.contextPath}/resources/plugins/h+/js/content.min.js?v=1.0.0"></script>
<script src="${ctxStatic}/plugins/h+/js/plugins/chosen/chosen.jquery.js"></script>
<script src="${pageContext.request.contextPath}/resources/plugins/h+/js/plugins/jsTree/jstree.js"></script>
<script src="${pageContext.request.contextPath}/resources/plugins/h+/js/plugins/layer2.4/layer.js"></script>
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>

<script>

    var ajaxpath='${pageContext.request.contextPath}';
    $(function () {
        $('#offlineCard_id').val('${id}');//当前实体卡id
        $('#offlineCard_cardTotal').html(numDiv('${offlineCard.cardTotal}',100)+'元');//券额：(单位:元)
        $('#syCount').html('${count}');//剩余数量

        loadData();//加载下拉列表
        $('#offlineCard_type').chosen({
            no_results_text : "未找到此选项!",
            width:"100%"
        });
        $("#offlineCard_type").val("1");
        changeType();

    });
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引


    function loadData()
    {
        $.ajax( {
            url:ajaxpath+'/offlineCard/doFQList.ac',// 跳转到 action
            data:{},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                loadBrand(data.listBrand);//商户列表
                loadType(data.listType);
                loadBizPerson(data.listBizPerson);

            },
            error : function() {
                alert("加载列表失败！");
            }
        });
    }

    //商户列表
    function loadBrand(listBrandObject){
        var html='<option value="">请选择</option>';
        for(var i=0;i<listBrandObject.length;i++)
        {

            html+='<option value="'+listBrandObject[i].id+'">'+listBrandObject[i].name+'</option>';
        }

        $("#list_brand_code").html(html);
        $('#list_brand_code').chosen({
            no_results_text : "未找到此选项!",
            width:"100%"
        });
        //默认选中某项
        /*if('${agent.role_code}'!=null&&'${agent.role_code}'!=''&&'${agent.role_code}'!=undefined)
        {
            $("#agent_role_code").val('${agent.role_code}');
            $("#agent_role_code").trigger("chosen:updated");
        }*/
    }

    //类型列表
    function loadType(listTypeObject){
        var html='<option value="">请选择</option>';
        for(var i=0;i<listTypeObject.length;i++)
        {

            html+='<option value="'+listTypeObject[i].id+'">'+listTypeObject[i].name+'</option>';
        }

        $("#list_type_code").html(html);
        $('#list_type_code').chosen({
            no_results_text : "未找到此选项!",
            width:"100%"
        });
    }

    //业务员代表
    function loadBizPerson(listBizPerson){
        var html='<option value="">请选择</option>';
        for(var i=0;i<listBizPerson.length;i++)
        {

            html+='<option value="'+listBizPerson[i].code+'">'+listBizPerson[i].person_name+'</option>';
        }

        $("#list_bizperson").html(html);
        $('#list_bizperson').chosen({
            no_results_text : "未找到此选项!",
            width:"100%"
        });
    }


    //保存
    function doSave()
    {
        var isOk = true;
        var brand_id=$("#list_brand_code").val();//商户
        var type_id=$("#list_type_code").val();//类型
        var countNumber=parseFloat($("#offlineCard_cardCount").val());//发放数量
        var syNumber=parseFloat($("#syCount").html());//剩余数量
        var notes = $("#list_card_notes").val();
        var type=$("#offlineCard_type").val();//是否制定发放
        var cardCode=$("#cardCode").val();//制定卡号
        var bizPersonId=$("#list_bizperson").val();//业务员


        $("#offlineCard_BrandId").val(brand_id);
        $("#offlineCard_TypeId").val(type_id);
        $("#offlineCard_notes").val(notes);
        $("#offlineCard_bizPersonId").val(bizPersonId);
        $("#cardCodeType").val(type);

        if(isNullOrEmpty(bizPersonId)){
            alert("请选择业务员");
            isOk = false;
        }
        if(isNullOrEmpty(brand_id)){
            alert("请选择商户");
            isOk = false;
        }
        if(isNullOrEmpty(type_id)){
            alert("请选择类型");
            isOk = false;
        }
        if(type=='1'){
            if(countNumber>syNumber){
                alert("发放数量不能大于剩余数量");
                isOk = false;
            }
        }else{
            if(cardCode==""||cardCode==null){
                alert("指定发放请选择发放号段");
                isOk = false;
            }
        }


        if(!isOk){
            return;
        }

        $.ajax({
            url : ajaxpath+'/offlineCard/doInsertGrand.ac',
            type: 'post',
            data:$('#offlineCardFaFangForm').serialize(),
            async : false,
            dataType: 'json',
            success:function(data){
                if(data=="success")
                {
                    alert("发放成功!");
                    parent.layer.close(index);
                }
                else  {
                        alert("发放失败!剩余数量不足");
                        $('#syCount').html(data)
                }

            }
        });

    }



    //关闭iframe
    $('#btn_close').click(function () {
        parent.layer.close(index);
    });



    //判断数量是否正确
    function checkNum(obj)
    {
        var syNumber=parseFloat($("#syCount").html());//剩余数量
        if(parseInt(obj.value)>syNumber)
        {
            alert("发放数量不可以超过剩余数量");
            obj.value=syNumber;
        }
    }


    ///////////////////////////公共方法////////////////////////////////////
    //true:是空 false:不为空
    function isNullOrEmpty(strVal) {
        if (strVal == null || strVal == undefined || strVal == '') {
            return true;
        } else {
            return false;
        }
    };

    /**
     * 除法运算，避免数据相除小数点后产生多位数和计算精度损失。
     *
     * @param num1被除数 | num2除数
     */
    function numDiv(num1, num2) {
        var baseNum1 = 0, baseNum2 = 0;
        var baseNum3, baseNum4;
        try {
            baseNum1 = num1.toString().split(".")[1].length;
        } catch (e) {
            baseNum1 = 0;
        }
        try {
            baseNum2 = num2.toString().split(".")[1].length;
        } catch (e) {
            baseNum2 = 0;
        }
        with (Math) {
            baseNum3 = Number(num1.toString().replace(".", ""));
            baseNum4 = Number(num2.toString().replace(".", ""));
            return (baseNum3 / baseNum4) * pow(10, baseNum2 - baseNum1);
        }
    };

//修改发放类型
    function changeType() {
        var type=$("#offlineCard_type").val();
        if(type==1){
            $("#type1").show();
            $("#type2").hide();
        }else{
            $("#type1").hide();
            $("#type2").show();
        }
    }
    
    //加载列表
    function  loadCard() {
        var id=$('#offlineCard_id').val();
        //自定页
        layer.open({
            type: 2,
            shift:4,//动画类型
            moveType:1,
            maxmin: true,
            shadeClose: true, //开启遮罩关闭
            area: ['800px', '490px'],
            content: ajaxpath+'/offlineCard/loadCard.ac?id='+id,
            success: function(layero, index){
                //调用成功执行
                //$table.bootstrapTable('refresh', queryParams);
                return true;
            },
            cancel: function(index){
                return true;
            },
            close  : function(index){

                $table.bootstrapTable('refresh', queryParams);
            }
        });
    }


    //保存制定卡号
    function  saveCardCode(id) {
        $("#cardCode").val(id.toString());
    }
</script>

</body>

</html>

